package com.lbz.utils;

import com.lbz.common.RedisPool;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;

/**
 * @Author 林必昭
 * @Date 2019/9/1 18:23
 */

@Slf4j
public class RedisPoolUtil {

    /**
     * 普通缓存放入
     * @param key
     * @param value
     * @return
     */
    public static String set(String key,String value){
        Jedis jedis = null;
        String result = null;

        try {
            jedis = RedisPool.getJedis();
            result = jedis.set(key,value);
        } catch (Exception e){
            log.error("set key:{},value:{},error",key,value,e);
            RedisPool.returnResource(jedis);
            return result;
        }
        RedisPool.returnResource(jedis);
        return result;
    }

    /**
     * 普通缓存获取
     * @param key
     * @return
     */
    public static String get(String key){
        Jedis jedis = null;
        String result = null;

        try {
            jedis = RedisPool.getJedis();
            result = jedis.get(key);
        } catch (Exception e){
            log.error("set key:{},error",key,e);
            RedisPool.returnResource(jedis);
            return result;
        }
        RedisPool.returnResource(jedis);
        return result;
    }

    /**
     * 删除缓存
     * @param key
     * @return
     */
    public static Long del(String key){
        Jedis jedis = null;
        Long result = null;

        try {
            jedis = RedisPool.getJedis();
            result = jedis.del(key);
        } catch (Exception e){
            log.error("del key:{},error",key,e);
            RedisPool.returnResource(jedis);
            return result;
        }
        RedisPool.returnResource(jedis);
        return result;
    }

    /**
     * 普通缓存放入并设置过期时间
     * @param key
     * @param exTimeSeconds    过期时间单位的是秒
     * @param value
     * @return
     */
    public static String setEx(String key,int exTimeSeconds,String value){
        Jedis jedis = null;
        String result = null;

        try {
            jedis = RedisPool.getJedis();
            result = jedis.setex(key,exTimeSeconds,value);
        } catch (Exception e){
            log.error("setex key:{},value:{},error",key,value,e);
            RedisPool.returnResource(jedis);
            return result;
        }
        RedisPool.returnResource(jedis);
        return result;
    }

    /**
     *
     * 设置key的有效期（单位是秒）
     *
     * 1: the timeout was set如果超时被设置返回1
     * 2: the timeout was not set since the key already has associated  如果没有超时返回0
     *
     * @param key
     * @param exTimeSeconds
     * @return
     */
    public static Long expire(String key,int exTimeSeconds){
        Jedis jedis = null;
        Long result = null;

        try {
            jedis = RedisPool.getJedis();
            result = jedis.expire(key,exTimeSeconds);
        } catch (Exception e){
            log.error("expire key:{},error",key,e);
            RedisPool.returnResource(jedis);
            return result;
        }
        RedisPool.returnResource(jedis);
        return result;
    }

    public static void main(String[] args) {
        Jedis jedis = RedisPool.getJedis();

        RedisPoolUtil.set("keyTest","valueTest");
        String v = RedisPoolUtil.get("keyTest");
        RedisPoolUtil.setEx("keyex",60*10,"valueex");   //有效期10分钟
        RedisPoolUtil.expire("keyTest",60*20);

        RedisPoolUtil.del("keyTest");

        System.out.println("end");
    }
}
